export default {
  //method 方法 ，url路由地址，params参数（json格式），port默认为主要服务地址
  https(method, url, params, port = "main") {
    method = method.toLowerCase();
    if (url.indexOf("https://") < 0 && url.indexOf("http://") < 0 && port != 'local') {
      url = this.configs[port] + url;
    }
    let responseType = "";
    if (url.indexOf("ExportToExcel") > 0) {
      responseType = "blob"
    }
    if (method == "get" || method == "delete") {
      let newparams = {};
      for (let key in params) {
        if (params[key] !== "" && params[key] !== "null" && params[key] !== "undefined" && params[key] !== undefined && params[key] !== null) {
          newparams[key] = params[key].toString().replace(/(^\s*)|(\s*$)/g, "");
        }
      }
      url = this.func.urlCode(url, newparams);
    }
    return new Promise((resolve, reject) => {
      this.$axios({
        method: method,
        url: url,
        data: params,
        responseType,
      }).then(res => {
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    });
  },
  httpUpload(method, url, params, process, port = "main") {
    method = method.toLowerCase();
    if (url.indexOf("https://") < 0 && url.indexOf("http://") < 0 && port != 'local') {
      url = this.configs[port] + url;
    }
    const formData = new FormData();
    for (let key in params) {
      formData.append(key, params[key]);
    }
    return new Promise((resolve, reject) => {
      this.$axios({
        method: method,
        url: url,
        data: formData,
        headers: {
          'Content-Type': 'multer/form-data'
        },
        onUploadProgress: (progressEvent) => {
          if (typeof process == "function" && progressEvent.lengthComputable) {
            process(progressEvent);
          }
        }
      }).then(res => {
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    });
  },
}
